package ttp.service;

import java.util.HashMap;

import ttp.dao.TtpDAO;
import ttp.to.CuttingTO;
import ttp.to.DryingTO;
import ttp.utilities.Utilities;

public class CuttingService {
	private TtpDAO dao;

	public CuttingService(TtpDAO dao) {
		super();
		this.dao = dao;
	}

	public HashMap run() throws Exception{
		long start_time = System.currentTimeMillis();
		CuttingTO cuto = new CuttingTO();
		DryingTO dto = dao.selectDryingByNo(0);
		if(dto!=null){
			int cuttingNo = dao.sequenceCutting();
			String enterPrise = dto.getEnterPrise();
			String treeType = dto.getTreeType();
			String treeGrade = dto.getTreeGrade();
			int paperWeight = dto.getPaperWeight();
			int paperCount = paperWeight*10;

			String productDate = Utilities.getNow();
			int cuttingCheck = (int) (Math.random()*100)+1;
			if(cuttingCheck==100){
				cuttingCheck = 1;
			}else{
				cuttingCheck = 0;
			}
			cuto = new CuttingTO(cuttingNo, enterPrise, treeType, treeGrade, paperCount, productDate, cuttingCheck, 0, 0);
			dao.insertCutting(cuto);
			dao.updateDrying(dto.getDryingNo());
			Thread.sleep(6000);
		}else{
			Thread.sleep(4000);
			cuto=null;
		}
		long end_time = System.currentTimeMillis();
		System.out.println("Cutting 처리시간: " + String.format("%,d msec", end_time - start_time));
		HashMap map = new HashMap();
		map.put("cuto", cuto);
		map.put("time", String.format("%,d msec", end_time - start_time));
		return map;
	}
}
